<template>
	<view class="content">
		<cu-custom bgColor="uerHead1" :isBack="true" textRight="true" type="1" @clickRight="save()">
			<block slot="backText" class="titilestyle">{{title}}</block>
			<block slot="right" class="" @click="save"><text class="text-blod text-blue">保存</text></block>
		</cu-custom>



		<scroll-view scroll-y="true">


			<view class="inro-card  flex  flex-direction margin-top">
				<view class="flex inro-card-fill  flex-wrap ">
					<view class="basis-xll margin-xs  text-cut text-gray">
						*学校
					</view>

					<view class="basis-xll margin-xs   flex align-center justify-between">
						<input class="basis-xl" placeholder="请输入" name="input" v-model="postData.school_name"></input>
						<!-- <view class="cuIcon-right"></view> -->
					</view>


				</view>
				<view class="flex inro-card-fill flex-wrap solid-top">
					<view class="basis-xll margin-xs  text-cut text-gray">
						*学历
					</view>

					<view class="basis-xll margin-xs text-xl  ">
						<picker mode="multiSelector" :range="edu" @change="selectEdu" @columnchange="MultiColumnChange">

							<view class=" flex  justify-between align-center">
								<view class="">
									<text class="text-gray" v-if="postData.school==''">请选择</text>
									<text class="" v-else>
										{{postData.school || ""}}
									</text>
								</view>
								<view class="">
									<text class="cuIcon-right"></text>
								</view>
							</view>

						</picker>
					</view>
				</view>
				<view class="flex inro-card-fill  flex-wrap ">
					<view class="basis-xll margin-xs  text-cut text-gray">
						*专业
					</view>

					<view class="basis-xll margin-xs   flex align-center justify-between">
						<input class="basis-xl" placeholder="请输入" name="input" v-model="postData.major"></input>
						<!-- <view class="cuIcon-right"></view> -->
					</view>


				</view>

				<view class="flex inro-card-fill flex-wrap solid-top">
					<view class="basis-xll margin-xs  text-cut text-gray">
						*时间段
					</view>

					<view class="basis-xll margin-xs text-xl flex ">

						<view class="basis-sm ">
							<picker mode="date" :value="postData.start_at" @change="DateChange($event,'start_at')">

								<view class=" flex  justify-between">
									<view class="">
										<text class="text-gray" v-if="postData.start_at==''">请选择开始时间</text>
										<text v-else>{{postData.start_at}}</text>
									</view>
									<view class="">
										<text class="cuIcon-right"></text>
									</view>
								</view>

							</picker>
						</view>

						<view class="basis-xs text-center   radius">
							至
						</view>

						<view class="basis-sm ">
							<picker mode="date" :value="postData.endTime" :start="postData.start_at"
								@change="DateChange($event,'end_at')">
								<view class=" flex  justify-between">
									<view class="">
										<text class="text-gray" v-if="postData.end_at==''">请选择结束时间</text>
										<text v-else>{{postData.end_at}}</text>

									</view>
									<view class="">
										<text class="cuIcon-right"></text>
									</view>
								</view>

							</picker>
						</view>




					</view>
				</view>



				<view class="flex inro-card-fill flex-wrap solid-top">
					<view class="basis-xll margin-xs  text-cut text-gray">
						*在校经历
					</view>

					<view class="basis-xll margin-xs">
						<textarea maxlength="300" style="width: 100%;" placeholder="请输入在校经历"
							v-model="postData.experience"></textarea>

					</view>

					<view class="basis-xll flex justify-end">
						{{postData.experience.length}}/{{300}}
					</view>
				</view>


			</view>


			<view class="padding flex basis-xl flex-wrap middle " v-if="ind!=-1">
				<button class="cu-btn lg bg-blue" style="width: 100%;" @click="del">删除该经历</button>
			</view>


		</scroll-view>




	</view>
</template>

<script>
	let edu = [

		{
			name: "小学",
			arr: []
		},
		{
			name: "高中",
			arr: []
		},
		{
			name: "大专",
			arr: ['全日制', '非全日制']
		},
		{
			name: "本科",
			arr: ['全日制', '非全日制']
		},
		{
			name: "硕士",
			arr: ['全日制', '非全日制']
		},
		{
			name: "博士",
			arr: ['全日制', '非全日制']
		}
	];
	export default {

		data() {

			return {
				title: "添加教育经历",
				date: '',
				edu: [
					[],
					[]
				],
				multiIndex: [-1, 0],
				postData: {
					school_name: "",
					school: "",
					major: "",
					start_at: "",
					end_at: "",
					experience: '',
					study_id: ""


				}, //教育经历
				ind: -1,




			}
		},
		onLoad(option) {
			if(typeof option.index!="undefined"){
				this.ind = option.index;
				this.getReseume();
			}
			
		},
		onReady() {
			for (let i in edu) {
				this.edu[0].push(edu[i].name);
			}

		},
		methods: {
			update(id, name) {
				console.log("name " + name);
				this.postData.school_name = name;
			},
			/**
			 * 获取我的简历
			 */
			getReseume() {

				this.apiRequest('/buyer/OfferResume', {

				}, "get", 0).then(res => {
					if (res.code == 200) {
						for (let i in this.postData) {

							if (i == "start_at" || i == "end_at") {
								let a = (res.data.resume.study[this.ind][i]).split(" ");
								res.data.resume.study[this.ind][i] = a[0];
							}

							this.postData[i] = res.data.resume.study[this.ind][i];

						}

					}


				})



			},

			/**
			 * 消失提示
			 * @param {Object} content
			 */
			showToast(content) {
				uni.hideLoading();
				uni.showToast({
					title: content,
					icon: "none"
				});

			},
			/**
			 * 接受选择参数
			 */
			backSelect(...dat) {
				this.postData.school = dat[0];
				console.log("index : " + dat[0]);
			},
			/**
			 * 选择学历
			 * @param {Object} e
			 */
			selectEdu(e) {
				this.multiIndex = e.detail.value;
				let project = "";
				(this.edu[1][this.multiIndex[1]]) ? (() => {
					project = this.edu[1][this.multiIndex[1]];
				})() : "";
				this.postData.school = this.edu[0][this.multiIndex[0]] + project;

			},
			MultiColumnChange(e) {
				switch (e.detail.column) {
					case 0: //第一列
						this.$set(this.edu, 1, edu[e.detail.value].arr);
						this.multiIndex[0] = e.detail.value;
						break;
					case 1: //第二列
						this.multiIndex[1] = e.detail.value;
						break;
				}


			},

			/**
			 * 选择学校
			 */
			select(type, title) {

				uni.navigateTo({
					url: "../../../../pages/basic/position?type=change"
				})
			},
			/**
			 * 选择时间段
			 */
			DateChange(e, type) {
				this.postData[type] = e.detail.value;
			},
			/**
			 * 验证表达式
			 */
			verify(...dat) {
				let val = true,
					da = dat[1][dat[0]];
				console.log(da);
				switch (dat[0]) {
					case "school_name": //关于username的验证
						if (da.length == 0) {
							this.showToast("请输入学校名称");
							val = false;
						}
						break;
					case "school":
						if (da.length == 0) {
							console.log(da)
							this.showToast("请选择学历");
							val = false
						}
						break;
					case "major":
						if (da == "") {
							this.showToast("请输入专业名称");
							val = false
						}
						break;
					case "start_at":

						if (da == "") {

							this.showToast("请选择开始时间");
							val = false
						}
						break;
					case "end_at":

						if (da == "") {

							this.showToast("请选择结束时间");
							val = false
						}
						break;

					case "experience":

						if (da == "") {

							this.showToast("请输入在校经历");
							val = false
						} else if (da.length > 300) {
							this.showToast("字数超出限制");
							val = false
						}
						break;
				}
				return val
			},
			/**
			 * 删除该经历
			 */
			del() {
				let d = () => {
					this.apiRequest('/buyer/OfferResumeDelStudy', {
						study_id: this.postData.study_id
					}, 1, "DELETE").then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: "删除成功",
								icon: "none"
							})
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 1000);



						}
					}).catch(() => {

					})
				}

				uni.showModal({
					title: "删除提示",
					content: "确认是否删除该经历？",
					success: (e) => {
						if (e.confirm) {
							d();
						}
					}
				})

			},

			/**
			 * 保存经历
			 */
			save() {
				let dat = this.postData;
				for (let u in dat) {
					let isPass = this.verify(u, dat);
					if (!isPass) {
						return false;
					}
				}

				let postInfo = {};
				postInfo.basic = this.postData;
				uni.setStorageSync("postInfo", postInfo);
				this.apiRequest('/buyer/OfferResumeEditStudy', this.postData, 1, "post").then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: "保存成功",
							icon: "success"
						})
						this.postInfo = res.data.resume.study;

					}
				}).catch(() => {

				})


			}





		}


	}
</script>

<style scoped>
	/**
	 * 卡片样式
	 */
	.inro-card {
		width: 92%;
		margin: 20upx auto;
		background: #FFFFFF;
		box-shadow: 0px 0px 10upx 0px rgba(0, 0, 0, 0.05);
		border-radius: 20upx;
		padding: 30upx;
		position: relative;
	}

	.inro-card-fill {
		width: 100%;
		margin: 20rpx 0 20rpx 0;
		min-height: 100rpx;
	}

	.text-right {
		position: absolute;
		float: right;
		right: 0;
	}

	/* 	picker {
		position: relative;
	} */
</style>
